package com.czmini.cztest.controller;

import com.alibaba.fastjson.JSONObject;
import com.czmini.cztest.common.TokenDb;
import com.czmini.cztest.config.UserBaseStr;
import com.czmini.cztest.dto.PageTableRequest;
import com.czmini.cztest.dto.PageTableResponse;
import com.czmini.cztest.dto.ResultDto;
import com.czmini.cztest.dto.TokenDto;
import com.czmini.cztest.dto.jenkins.AddZcJenkinsTestUserDto;
import com.czmini.cztest.dto.jenkins.QueryZcTestJenkinsListDto;
import com.czmini.cztest.dto.jenkins.UpdateZcJenkinsTestUserDto;
import com.czmini.cztest.entity.ZcTestJenkins;
import com.czmini.cztest.service.ZcTestJenkinsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.controller
 * @ClassName: ZcJenkinsTestController
 * @Author: 还是那个橙子
 * @Description: Jenkins管理模快
 * @Date: 2022/3/16 21:36
 * @Version: 1.0
 */
@Api(tags = "zctest-jenkins管理")
@RequestMapping("/jenkins")
@Slf4j
@RestController  //将类标记成对外服务的web类  spring会转换返回值并自动写入到http响 应中
public class ZcJenkinsTestController {

    @Autowired
    private ZcTestJenkinsService zcTestJenkinsService;

    @Autowired
    private TokenDb tokenDb;

    @ApiOperation("添加jenkins接口")
    @PostMapping
    public ResultDto<ZcTestJenkins> save(HttpServletRequest request, @RequestBody AddZcJenkinsTestUserDto addZcJenkinsTestUserDto){

        log.info("添加Jenkins-入参= "+ JSONObject.toJSONString(addZcJenkinsTestUserDto));

        //非空判断
        if (Objects.isNull(addZcJenkinsTestUserDto)){
            return  ResultDto.fail("请参不能为空");
        }
        String name = addZcJenkinsTestUserDto.getName();

        if (StringUtils.isEmpty(name)){
            return  ResultDto.fail("Jenkins名称不能为空");
        }

        // 实体类  数据库操作层
        ZcTestJenkins zcTestJenkins = new ZcTestJenkins();
        // 复制属性到对应实体类
        BeanUtils.copyProperties(addZcJenkinsTestUserDto,zcTestJenkins);

        //从客户端请求的header中获取token，并根据token获取用户信息 -- 为什么要从这里获取？？？
        TokenDto tokendto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        System.out.println(tokendto);
        //
        zcTestJenkins.setCreateUserId(tokendto.getUserId());
        //获取后缀  过滤带.的后缀，如.yml改为yml
        String commandRunCaseSuffix = addZcJenkinsTestUserDto.getCommandRunCaseSuffix();
        if(!org.springframework.util.StringUtils.isEmpty(commandRunCaseSuffix)){
            zcTestJenkins.setCommandRunCaseSuffix(commandRunCaseSuffix.replace(".",""));
        }

        log.info("=== "+ JSONObject.toJSONString(zcTestJenkins));
        ResultDto<ZcTestJenkins> resultDto=zcTestJenkinsService.save(tokendto, zcTestJenkins);
        return resultDto;
    }


    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public ResultDto<PageTableResponse<ZcTestJenkins>> list(HttpServletRequest request,
                                                            PageTableRequest<QueryZcTestJenkinsListDto> pageTableRequest){
        if (Objects.isNull(pageTableRequest)){
            return ResultDto.success("列表查询参数不能为空");
        }
        // 登录验证
        TokenDto tokenDto= tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        log.info("Jenkins列表查询-入参= "+ JSONObject.toJSONString(pageTableRequest));
        // 获取参数
        QueryZcTestJenkinsListDto params = pageTableRequest.getParams();
        if (Objects.isNull(params)){
            params= new QueryZcTestJenkinsListDto();
        }
        params.setCreateUserId(tokenDto.getUserId());
        pageTableRequest.setParams(params);
        // 列表查询
        ResultDto<PageTableResponse<ZcTestJenkins>> responseResultDto = zcTestJenkinsService.list(pageTableRequest);
        return  responseResultDto;
    }


    @ApiOperation(value = "根据jenkinsId删除")
    @DeleteMapping("/{jenkinsId}")
    public ResultDto<ZcTestJenkins> delete(HttpServletRequest request, @PathVariable Integer jenkinsId) {
        log.info("根据jenkinsId删除-入参= "+ jenkinsId);

        if(Objects.isNull(jenkinsId)){
            return ResultDto.success("JenkinsId不能为空");
        }

        TokenDto tokendto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));

        ResultDto<ZcTestJenkins> resultDto = zcTestJenkinsService.delete(jenkinsId, tokendto);
        return resultDto;

    }


    @ApiOperation(value = "根据jenkinsId查询")
    @GetMapping ("/{jenkinsId}")
    public ResultDto<ZcTestJenkins> queryById(HttpServletRequest request, @PathVariable Integer jenkinsId) {
        log.info("根据jenkinsId删除-入参= "+ jenkinsId);

        if(Objects.isNull(jenkinsId)){
            return ResultDto.success("JenkinsId不能为空");
        }
        TokenDto tokendto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        ResultDto<ZcTestJenkins> resultDto = zcTestJenkinsService.delete(jenkinsId, tokendto);
        return resultDto;

    }


    @ApiOperation("修改jenkins接口")
    @PutMapping
    public ResultDto<ZcTestJenkins> update(HttpServletRequest request, @RequestBody UpdateZcJenkinsTestUserDto updateZcJenkinsTestUserDto ){
        log.info("添加Jenkins-入参= "+ JSONObject.toJSONString(updateZcJenkinsTestUserDto));

        //非空判断
        if (Objects.isNull(updateZcJenkinsTestUserDto)){
            return  ResultDto.fail("请参不能为空");
        }
        Integer jenkinsTestUserDtoId = updateZcJenkinsTestUserDto.getId();
        String name=updateZcJenkinsTestUserDto.getName();


        if (StringUtils.isEmpty(name)){
            return  ResultDto.fail("Jenkins名称不能为空");
        }
        if (Objects.isNull(jenkinsTestUserDtoId)){
            return ResultDto.success("JenkinsId不能为空");
        }

        // 实体类  数据库操作层
        ZcTestJenkins zcTestJenkins = new ZcTestJenkins();
        // 复制属性到对应实体类
        BeanUtils.copyProperties(updateZcJenkinsTestUserDto,zcTestJenkins);

        //从客户端请求的header中获取token，并根据token获取用户信息 -- 为什么要从这里获取？？？
        TokenDto tokendto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        System.out.println(tokendto);
        //
        zcTestJenkins.setCreateUserId(tokendto.getUserId());
        //获取后缀  过滤带.的后缀，如.yml改为yml
        String commandRunCaseSuffix = updateZcJenkinsTestUserDto.getCommandRunCaseSuffix();
        if (Objects.nonNull(commandRunCaseSuffix)&&commandRunCaseSuffix.startsWith(".")){
            commandRunCaseSuffix=commandRunCaseSuffix.replace(".","");
        }

        updateZcJenkinsTestUserDto.setCommandRunCaseSuffix(commandRunCaseSuffix);
        log.info("=== "+ JSONObject.toJSONString(zcTestJenkins));
        ResultDto<ZcTestJenkins> resultDto=zcTestJenkinsService.update(tokendto, zcTestJenkins);
        return resultDto;
    }





}
